/*

* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     Smart-Shop开源商城系统/ Java商城-首选电商平台系统 请务必保留此注释头信息
* |     开源地址: https://gitee.com/ningbo-qishan/gruul-mall
* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     可正常分享和学习源码,未经授权 不可商用！
* |     商业版中台购买/开源版商业授权 联系技术客服
* |     官网:  https://www.73app.cn/
* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     Copyright (c) 2016-2999 宁波启山智软科技有限公司
* |     版权所有 ,侵权必究！
* ------------------------------------------------------------------------

*/
package com.medusa.gruul.payment.handler.status;

import cn.hutool.core.util.StrUtil;
import com.medusa.gruul.common.core.exception.ServiceException;
import com.medusa.gruul.common.core.handler.Handler;
import com.medusa.gruul.common.core.util.SystemCode;
import com.medusa.gruul.payment.common.enums.TradeStatus;
import com.medusa.gruul.payment.common.model.OrderPaymentRecord;
import com.medusa.gruul.payment.service.MultiPayOrderService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * 支付状态校验器
 * @author 张治保
 * date 2021/12/23
 */
@Slf4j
@RequiredArgsConstructor
public abstract class AbstractPayStatusVerifyHandler implements Handler<TradeStatus> {

    private final MultiPayOrderService multiPayOrderService;

    @Override
    public TradeStatus handle(Object... params) {

        if (hasErrorParam(params, String.class)){
            throw new ServiceException(SystemCode.PARAM_BIND_ERROR);
        }
        String outTradeNo = cast(params[0],String.class);

        if (StrUtil.isBlank(outTradeNo) ) {
            throw new ServiceException(SystemCode.DATA_NOT_EXIST);
        }
        OrderPaymentRecord paymentRecord = multiPayOrderService.paymentRecord(outTradeNo);

        TradeStatus status = handle(paymentRecord.getDetailsId(), outTradeNo);
        log.info("支付状态检查结果:{}",status);
        return status;
    }

    protected abstract TradeStatus handle(String detailsId,String outTradeNo);
}
